<?php

namespace App\Http\Controllers;

use  App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class AdminController extends Controller
{
    //abc12357
    private $salt;

    public function __construct()
    {
        $this->salt = "qiman";
    }

//    public function regin()
//    {
//        $user = new User;
//        $user->username = 'admin';
//        $user->password = sha1($this->salt.'abc12357');
//        $user->email = '849140750@qq.com';
//        $user->api_token = str_random(60);
//        if($user->save()) {
//            echo  '用户注册成功!';
//        }
//    }


    public function index()
    {
        return view('admin/index');
    }

    public function login(Request $request)
    {
        if($request->has('username') && $request->has('password')){
            $user = User::where('username', '=', $request->input('username'))->where('password', '=', sha1($this->salt.$request->input('password')))->first();

            if($user){
                $token = str_random(60);
                $user->api_token = $token;
                $user->save();
//                return response()->json(['status'=>1,'api_token'=>$token]);
                return redirect('/admin/message?api_token='.$token);

            }
        }
        return redirect('/admin');
    }

    public function message()
    {
        $message = DB::table('message')->orderBy('id','desc')->simplePaginate(15);
        return view('admin/message',['message'=>$message]);
    }

}
